# 修改密码
# 用户取款界面
from PyQt5 import QtWidgets, QtGui
from mysql import select_sql, updata_sql
import temp


class user_update_password(QtWidgets.QMainWindow):
    def __init__(self):
        super(user_update_password, self).__init__()
        self.card = temp.card

    def setupUI(self):
        self.setWindowTitle("修改密码")
        self.setGeometry(100, 100, 300, 200)
        self.Qlable1()
        self.Qlable2()
        self.Qlable3()
        self.Qlineedit1()
        self.Qlineedit2()
        self.Qlineedit3()
        self.Qbutton1()
        self.Qbutton2()

    def Qlable1(self):
        qlable = QtWidgets.QLabel(self)
        qlable.setText("请输入原始密码")
        qlable.setGeometry(30, 30, 100, 30)

    def Qlineedit1(self):
        self.qlineedit1 = QtWidgets.QLineEdit(self)
        self.qlineedit1.setGeometry(150, 30, 100, 30)
        self.qlineedit1.setValidator(QtGui.QIntValidator(self))
        self.qlineedit1.setMaxLength(6)

    def Qlable2(self):
        qlable = QtWidgets.QLabel(self)
        qlable.setText("请输入新密码：")
        qlable.setGeometry(30, 80, 100, 30)

    def Qlineedit2(self):
        self.qlineedit2 = QtWidgets.QLineEdit(self)
        self.qlineedit2.setGeometry(150, 80, 100, 30)
        self.qlineedit2.setValidator(QtGui.QIntValidator(self))
        self.qlineedit2.setMaxLength(6)

    def Qlable3(self):
        qlable = QtWidgets.QLabel(self)
        qlable.setText("请在输入一次密码：")
        qlable.setGeometry(30, 130, 100, 30)

    def Qlineedit3(self):
        self.qlineedit3 = QtWidgets.QLineEdit(self)
        self.qlineedit3.setGeometry(150, 130, 100, 30)
        self.qlineedit3.setValidator(QtGui.QIntValidator(self))
        self.qlineedit3.setMaxLength(6)

    def Qbutton1(self):
        qbutton = QtWidgets.QPushButton(self)
        qbutton.setText("修改密码")
        qbutton.setGeometry(30, 160, 100, 30)
        qbutton.clicked.connect(self.Qbutton1_click)

    # 输入内容的验证
    # 验证密码长度
    def clean_password(self,password):
        if len(password) < 6 or password == '':
            return 0
        return 1

    # 验证旧密码是否正确
    def clean_old_password(self,old_password):
        sql = "SELECT * FROM ATM.User where card = '{0}' and password = '{1}'".format(self.card,old_password)
        result = select_sql(sql)
        if result == None :
            return 0
        return 1

    def Qbutton1_click(self):
        old_password = self.qlineedit1.text()
        new_password = self.qlineedit2.text()
        new_password2 =self.qlineedit3.text()
        clean1 = self.clean_password(old_password)
        clean2 = self.clean_password(new_password)
        clean3 = self.clean_password(new_password2)
        if clean1 == 0 or clean2 == 0 or clean3==0:
            self.msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, "警告", "输入的内容小于6位或为空")
            return self.msg.show()
        else:
            if new_password != new_password2:
                self.msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, "警告",
                                                 "两次输入的密码不一致")
                return self.msg.show()
            else:
                clean5 = self.clean_old_password(old_password)
                if clean5 == 0 :
                    self.msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, "警告",
                                                     "旧密码输入错误")
                    return self.msg.show()
                else:
                    # 修改密码
                    sql = "UPDATE ATM.User SET password = '{0}' WHERE card = '{1}' ".format(new_password,self.card)
                    updata_sql(sql)
                    # 成功后回到主窗口
                    self.msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Information, "提示",
                                                     "修改成功")
                    return self.msg.show()

    def Qbutton2(self):
        qbutton = QtWidgets.QPushButton(self)
        qbutton.setText("返回")
        qbutton.setGeometry(150, 160, 100, 30)
        qbutton.clicked.connect(self.Qbutton2_click)

    def Qbutton2_click(self):
        self.close()
        # 返回后，主窗口不解除限制
        from user_main import user_main
        self.a = user_main()
        self.a.setupUI()
        self.a.show()
